import { createStore, Store ,useStore as VuexStore } from "vuex";
import login from "./login/login";
import system from "./main/system/system"
import analysis from "./main/analysis/analysis"
import { IRootState, IStoreType } from "./types";
import { getUserListRequest } from "@/service/main/system/system"

const store = createStore<IRootState>({
  state() {
    return {
      // name: "111",
      // age: 15,
      //完整数据
      roleList:[],
      departmentList:[],
      menuList:[]
    };
  },
  mutations: {
    changeDepartment(state,list){
      state.departmentList = list
    },

    changeRole(state,list){
      state.roleList = list
    },

    changeMenu(state,list){
      state.menuList = list
    }
  },
  getters: {},
  actions: {
    // 发起部门和角色数据的请求
    async getInitDataAction({commit}){
      // 获取部门列表的数据
      const departmentReasult = await getUserListRequest('/department/list',{offset:0,size:100})
      const { list:departmentList } = departmentReasult.data
      commit('changeDepartment',departmentList)

      // 获取角色列表的数据
      const roleReasult = await getUserListRequest('/role/list',{offset:0,size:100})
      //给 list 取一个别名
      const { list:roleList } = roleReasult.data
      
      commit('changeRole',roleList)

      // 获取菜单列表数据
      const menuResult = await getUserListRequest('/menu/list',{
        offset:0,
        size:100
      })
      const { list:menuList } = menuResult.data
      
      commit('changeMenu',menuList)

    }

  },
  modules: {
    login,
    system,
    analysis
  },
});

export function setupStore(){
    store.dispatch('login/loadLocalLogin')
    //调用函数
    
}

// 将组合接口模型使用进行类型限制
export function useStore(): Store<IStoreType> {
  return VuexStore()
}
export default store
